package com.bs.resa.service.impl;

import com.bs.resa.dao.ModuleMapper;
import com.bs.resa.dao.PTypeMapper;
import com.bs.resa.dao.TaskMapper;
import com.bs.resa.pojo.PType;
import com.bs.resa.service.ProjectTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lin
 * @date 2019/3/22 13:28
 */
@Service
public class ProjectTypeServiceImpl implements ProjectTypeService {
    @Resource
    PTypeMapper pTypeMapper;
    @Resource
    TaskMapper taskMapper;
    /**
     * 查询项目的类型
     *
     * @return
     */
    @Override
    public List<PType> getProjectTypeList() {
        List<PType> pTypes=pTypeMapper.getProjectTypeList();
        for(PType pType:pTypes){
            pType.setTname(pType.getIcon()+pType.getTname());
        }
        return pTypes;
    }

    /**
     * 任务类型统计
     * @param pid
     * @return
     */
    @Override
    public List<Map<String, Object>> queryProjectTypeByPid(String pid) {
        List<Map<String,Object>> listType=taskMapper.queryTaskType();
        List<Map<String, Object>> list=new ArrayList<>();
        for(Map<String,Object> tType:listType){
            Map<String, Object> map=new HashMap<>();

            map.put("tType",tType);
            //通过tid 和pid获得当前类型的数量统计
            int i= taskMapper.getCountTaskType((Long)tType.get("id"),pid);
            //总任务数
            int sum=taskMapper.getCountTask(pid);
            map.put("count",i);
            if(i!=0&& sum!=0){
                map.put("sum",(((float)i/sum)*100)+"%");
            }else{
                map.put("sum","0%");
            }
            list.add(map);

        }

        return list;
    }

    /**
     * 状态类型统计
     *
     * @param pid
     * @return
     */
    @Override
    public List<Map<String, Object>> queryProjectStatus(String pid) {
        List<Map<String,Object>> listType=taskMapper.queryTaskStatusList();
        for(Map<String ,Object> staus:listType){
            int i=taskMapper.getCountTaskStatus(pid,(Long)staus.get("pTid"));
            staus.put("count",i);
            //总任务数
            int sum=taskMapper.getCountTask(pid);
            if(i!=0&& sum!=0){
                staus.put("sum",(((float)i/sum)*100)+"%");
            }else{
                staus.put("sum","0%");
            }
            staus.put("count",i);
        }

        return listType;
    }

    /**
     * 统计数据
     *
     * @param pid
     * @return
     */
    @Override
    public List<Map<String, Object>> queryCount(String pid) {
        //获得类型
        List<Map<String,Object>> listType=taskMapper.queryTaskType();

        //获得状态
        List<Map<String,Object>> listStatus=taskMapper.queryTaskStatusList();

        for(Map<String,Object> map:listType){
            Map<String,Object> sum=new HashMap<>();
            int i=0;
            //统计不同类型的不同状态的数量
            for(Map<String,Object> statusMap:listStatus){
                i++;
                sum.put("count"+i,taskMapper.getSum(pid,(Long)map.get("id"),(Long)statusMap.get("pTid")));
            }
            map.put("data",sum);
        }
        return listType;
    }


}
